package com.jieao.content.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jieao.common.core.domain.Ztree;
import com.jieao.common.core.text.Convert;
import com.jieao.content.domain.Article;
import com.jieao.content.domain.ArticleLike;
import com.jieao.content.domain.ArticleSub;
import com.jieao.content.dto.ArticleDto;
import com.jieao.content.mapper.ArticleLikeMapper;
import com.jieao.content.mapper.ArticleMapper;
import com.jieao.content.mapper.ArticleSubMapper;
import com.jieao.content.service.IArticleService;
import com.jieao.framework.util.ShiroUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Author weifanl
 * @Date2020/5/21 15:33
 **/
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {
    @Autowired
    private ArticleSubMapper articleSubMapper;
    @Autowired
    private ArticleLikeMapper articleLikeMapper;

    @Override
    public List<ArticleDto> selectArticleList(ArticleDto article) {
        return baseMapper.selectArticleList(article);
    }

    @Override
    public List<ArticleDto> selectFrontArticleList(ArticleDto article) {
        List<ArticleDto> articleDtos = baseMapper.selectFrontArticleList(article);
        /*for (ArticleDto articleDto : articleDtos) {
            String content = articleDto.getContent();
            while (content.indexOf("![](") >= 0) {
                int imgStart = content.indexOf("![](");
                content = content.replace(content.substring(imgStart, content.indexOf(")", imgStart) + 1), "");
            }
            Integer end = (content.length()-1 > 2000 ? 2000:content.length()-1);
            content = content.substring(0,end) + "...";
            articleDto.setContent(content);
        }*/
        return articleDtos;
    }

    @Transactional
    @Override
    public int insertArticle(ArticleDto article) {
        //插入article_sub
        ArticleSub articleSub = article.createArticleSub();
        articleSub.setCreateBy(ShiroUtils.getLoginName());
        articleSub.setCreateTime(new Date());
        articleSubMapper.insert(articleSub);

        //插入article
        article.setSubId(articleSub.getId());
        if(article.getIsRelease().equals("Y")){
            article.setReleaseTime(new Date());
        }
        int insert = baseMapper.insert(article);
        //修改 -上篇文章
        Article lastArticle = baseMapper.selectById(article.getLastArticleId());
        if(null != lastArticle){
            lastArticle.setNextArticleId(article.getId());
            baseMapper.updateById(lastArticle);
        }
        //修改 -下篇文章
        Article nextArticle = baseMapper.selectById(article.getNextArticleId());
        if(null != nextArticle){
            nextArticle.setLastArticleId(article.getId());
            baseMapper.updateById(nextArticle);
        }
        return insert;
    }

    @Override
    public ArticleDto selectArticleById(Long articleId) {
        ArticleDto article = baseMapper.selectArticleById(articleId, ShiroUtils.getUserId());
        return article;
    }

    @Transactional
    @Override
    public int updateArticle(ArticleDto articleDto) {

        ArticleSub articleSub = articleDto.createArticleSub();
        articleSub.setId(articleDto.getSubId());
        articleSubMapper.updateContentById(articleSub);

        Article article = new Article();
        BeanUtils.copyProperties(articleDto, article);

        if(article.getIsRelease().equals("Y")){
            article.setReleaseTime(new Date());
        }
        int i = baseMapper.updateById(article);

        //修改 -上篇文章
        Article lastArticle = baseMapper.selectById(article.getLastArticleId());
        if(null != lastArticle){
            lastArticle.setNextArticleId(article.getId());
            baseMapper.updateById(lastArticle);
        }
        //修改 -下篇文章
        Article nextArticle = baseMapper.selectById(article.getNextArticleId());
        if(null != nextArticle){
            nextArticle.setLastArticleId(article.getId());
            baseMapper.updateById(nextArticle);
        }
        return i;
    }

    @Override
    public int deleteArticleByIds(String ids) {
        int i = 0;
        Long[] articleIds = Convert.toLongArray(ids);
        for (Long articleId : articleIds) {
            //Article article = baseMapper.selectById(articleId);
            baseMapper.deleteArticleById(articleId);
            //逻辑删除，文章保存
            //articleSubMapper.deleteById(article.getSubId());
            ++i;
        }

        return i;
    }

    @Override
    public int change(ArticleDto catalog) {
        return baseMapper.change(catalog);
    }


    /**
     * 查询目录管理树
     *
     * @param article 目录信息
     * @return 所有目录信息
     */
    @Override
    public List<Ztree> selectArticleTree(ArticleDto article) {
        List<ArticleDto> articleList = baseMapper.selectArticleTreeList(article);
        List<Ztree> ztrees = initZtree(articleList);
        return ztrees;
    }

    /**
     * 对象转目录树
     *
     * @param articleList 文章列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<ArticleDto> articleList) {

        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (ArticleDto article : articleList) {
            Ztree ztree = new Ztree();
            ztree.setId(article.getId());
            ztree.setpId(article.getParentId());
            ztree.setName(article.getTitle());
            ztree.setTitle(article.getTitle());
            ztree.setNocheck(article.isNocheck());
            ztrees.add(ztree);
        }
        return ztrees;
    }

    /**
     * 喜欢文章
     *
     * @Author LWF
     * @Date 17:12 2020/8/31
     **/
    @Override
    public Integer articleLike(Long articleId) {
        ArticleLike artLike = articleLikeMapper.selectOne(Wrappers.<ArticleLike>query().lambda()
                .eq(ArticleLike::getArticleId, articleId)
                .eq(ArticleLike::getUserId, ShiroUtils.getUserId()));

        if (null == artLike) {
            ArticleLike like = new ArticleLike();
            like.setArticleId(articleId);
            like.setUserId(ShiroUtils.getUserId());
            return articleLikeMapper.insert(like);
        }
        return 0;
    }
}
